﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace NewsletterCreator
{
    [DefaultProperty("Text")]
    [ToolboxData("<{0}:NumberedNewsletterItem runat=server></{0}:NumberedNewsletterItem>")]
    public class NumberedNewsletterItem : HtmlableControl
    {
        Label lblTitle = new Label();
        private DropDownList ddlNumItems = new DropDownList();

        private string title = "default";
        private DataTable datasource;

        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string Text
        {
            get
            {
                String s = (String)ViewState["Text"];
                return ((s == null) ? String.Empty : s);
            }

            set
            {
                ViewState["Text"] = value;
            }
        }

        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string Title
        {
            get
            {
                return title;
            }

            set
            {
                title = value;
            }
        }
        [Bindable(true)]
        [Category("Data")]
        [DefaultValue("")]
        [Localizable(true)]
        public DataTable Datasource
        {
            get
            {
                return datasource;
            }

            set
            {
                datasource = value;
            }
        }

        protected override void Render(HtmlTextWriter output)
        {
            base.Render(output);
        }

        protected override void CreateChildControls()
        {
            lblTitle.Text = "<b>" + title + "</b>";

            Label lblNumItems = new Label();
            lblNumItems.Text = "Number of items to display: ";

            for(int i=1; i<=20;i++)
                ddlNumItems.Items.Add(new ListItem(""+i));
            ddlNumItems.SelectedValue = "10";

            Panel pnlLayout = new Panel();
            pnlLayout.BorderWidth = 1;

            pnlLayout.Controls.Add(lblTitle);
            pnlLayout.Controls.Add(new LiteralControl("<BR />"));
            pnlLayout.Controls.Add(lblNumItems);
            pnlLayout.Controls.Add(ddlNumItems);

            Controls.Clear();
            Controls.Add(pnlLayout);
        }

        public override string toHTML()
        {
            string str = "<h4>"+title+"</h4>";
            //str += "<br />" + ddlNumItems.SelectedValue;
            int num = ddlNumItems.SelectedIndex;
            str += "<ul>";
            for (int i = 0; i <= num && i < datasource.Rows.Count; i++)
            {
                str += "<li><b>" + datasource.Rows[i].ItemArray[0] + "</b><br />";
                                
                for(int j=1;j<+datasource.Rows[i].ItemArray.Length;j++)
                    str += datasource.Rows[i].ItemArray[j]+"<br />";
                str += "</li>";
            }
            str += "</ul>";

            return str;
        }
    }
}
